package com.cscp.javastudy.basic.reflect;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

/**
 * 通过反射获取泛型
 */
public class GenericType {

    public static void main(String[] args) throws NoSuchMethodException {
        // 获取入参的泛型类型
        Method test01 = GenericType.class.getDeclaredMethod("test01", Map.class, List.class);
        Type[] genericParameterTypes = test01.getGenericParameterTypes();
        for (Type genericType : genericParameterTypes) {
//            System.out.println(genericType);
            if (genericType instanceof ParameterizedType) {
                Type[] actualTypeArguments = ((ParameterizedType) genericType).getActualTypeArguments();
                for (Type actualTypeArgument : actualTypeArguments) {
                    System.out.println(actualTypeArgument);
                }
            }
        }

        // 获取返回的泛型类型
        Method test02 = GenericType.class.getDeclaredMethod("test02", null);
        Type genericReturnType = test02.getGenericReturnType();
        if (genericReturnType instanceof ParameterizedType) {
            Type[] actualTypeArguments = ((ParameterizedType) genericReturnType).getActualTypeArguments();
            for (Type actualTypeArgument : actualTypeArguments) {
                System.out.println(actualTypeArgument);
            }
        }
    }

    public void test01(Map<String, User> map, List<User> list){
        System.out.println("test01()");
    }

    public Map<String, User> test02(){
        System.out.println("test02()");
        return null;
    }


}
